//
//  BaseViewController.swift
//  WeiBo
//
//  Created by Gao on 16/3/2.
//  Copyright © 2016年 Gao. All rights reserved.
//

import UIKit
/// 所用控制器的基类
class BaseViewController: UITableViewController,VisitorViewDelegate
{
    /// 是否登陆
    var isLogin:Bool = true
    /// 保存登录界面
    var visitorView:VisitorView?
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    override func loadView()
    {
        isLogin ? super.loadView() : setupVisitorView()
    }
    
    /**
     配置每个控制器未登录时的显示页面
    */
    func setupVisitorView()
    {
        let visitview = VisitorView()
            visitview.delegate = self
            visitorView = visitview
        self.view = visitview

//        navigationController!.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Done, target: self, action: Selector("registerButtonWillClick"))
//        navigationController!.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .Done, target: self, action: Selector("loginButtonWillClick"))
    }
    
    /**
     判断是不是首页，因为每个界面未登录的时候显示的东西不一样
     
     - parameter isHome:     是否主页
     - parameter imageNamed: 图标名字
     - parameter message:    图标下面的label文字
     */
    func configLoginView(isHome:Bool,imageNamed:String,message:String)
    {
        visitorView?.IconView.hidden = !isHome
        visitorView?.homeIconView.image = UIImage(named: imageNamed)
        visitorView?.messageLabel.text = message
        
        /**
        *  如果是主页，后面的转盘开始转动
        */
        if isHome
        {
            startRotation()
        }
    }
    
    
    func loginButtonWillClick()
    {
        print("login")
    }
    
    func registerButtonWillClick()
    {
        print("register")
    }
    
    func startRotation()
    {
        //1.生成动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //2.动画的配置
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        //当离开页面后想要动画继续转动需要设置成false
        animation.removedOnCompletion = false
        //3.动画的添加
        visitorView?.IconView.layer.addAnimation(animation, forKey: nil)
    }
}
